/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package smartgym.gui;

import java.sql.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManagerFactory;
import javax.swing.JOptionPane;
import smartgym.controllers.PaymentJpaController;
import smartgym.controllers.exceptions.NonexistentEntityException;
import smartgym.gui.tables.PaymentTableFrame;
import smartgym.models.entities.Client;
import smartgym.models.entities.Employee;
import smartgym.models.entities.Manager;
import smartgym.models.entities.Payment;

/**
 *
 * @author Carlos
 */
public class PaymentWindow extends javax.swing.JDialog {

    // Entities    
    private Client client = null;
    private Employee employee = null;
    // Controller    
    private PaymentJpaController paymentController;
    // Tables
    private PaymentTableFrame paymentTable;
    

    /**
     * Creates new form PaymentWindow
     */
    public PaymentWindow(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    public PaymentWindow(MainWindow parent, boolean modal, EntityManagerFactory entityManagerFactory, Employee employee) {
        this(parent, modal);
        paymentController = new PaymentJpaController(entityManagerFactory);
        paymentTable = new PaymentTableFrame(); 
        tableFrame.add(paymentTable);
        this.employee = employee;

    }

    public void setClient(Client client) {
        this.client = client;
        restart();
    }

    public void restart() {
        if (client != null) {
            clientNameTextField.setText(this.client.getName());
            paymentTable.setObjectList(paymentController.findPaymentInadiplentByClient(client));
        } else {
            JOptionPane.showMessageDialog(this, "Não há client disponivel para esta janela");
            this.dispose();
        }

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        clientNameLabel = new javax.swing.JLabel();
        tableFrame = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        cancelButton = new javax.swing.JButton();
        payButton = new javax.swing.JButton();
        clientNameTextField = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        clientNameLabel.setText("Nome:");

        tableFrame.setBorder(javax.swing.BorderFactory.createTitledBorder("Pagamento Pendentes"));
        tableFrame.setLayout(new java.awt.BorderLayout());

        cancelButton.setText("Fechar");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        payButton.setText("Receber");
        payButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                payButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addComponent(payButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(cancelButton))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(payButton)))
        );

        clientNameTextField.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        clientNameTextField.setText("<nome de Fulano>");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tableFrame, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(clientNameLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(clientNameTextField)
                        .addGap(0, 236, Short.MAX_VALUE))
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(clientNameLabel)
                    .addComponent(clientNameTextField))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tableFrame, javax.swing.GroupLayout.DEFAULT_SIZE, 224, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void payButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_payButtonActionPerformed
        // pega pagamento selecionado
        Payment payment = (Payment) paymentTable.getSelectRow();
        
        if(payment==null){
            JOptionPane.showMessageDialog(this, "Nenhuma linha selecionada");
            return;
        }
        int resp = JOptionPane.showConfirmDialog(this, "Confirma o recebimento de pagamento?", "Confirme", JOptionPane.YES_NO_OPTION);

        if(resp == JOptionPane.YES_OPTION){
            payment.setEmployee(employee);
            payment.setPaid(true);
            java.util.Date date = new java.util.Date();
            payment.setPaymentedDay(new Date(date.getTime()));
            try {
                paymentController.edit(payment);
            } catch (NonexistentEntityException ex) {
                Logger.getLogger(PaymentWindow.class.getName()).log(Level.SEVERE, null, ex);
            } catch (Exception ex) {
                Logger.getLogger(PaymentWindow.class.getName()).log(Level.SEVERE, null, ex);
            }finally{
                JOptionPane.showMessageDialog(this, "Pagamento resgistrado com sucesso.");
                restart();
            }
            
        }else{
            JOptionPane.showMessageDialog(this, "Operação cancelada.\n Nehuma alteração foi realizada");
        }
        
    }//GEN-LAST:event_payButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        // TODO add your handling code here:
        cancel();
    }//GEN-LAST:event_cancelButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PaymentWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PaymentWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PaymentWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PaymentWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                PaymentWindow dialog = new PaymentWindow(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel clientNameLabel;
    private javax.swing.JLabel clientNameTextField;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JButton payButton;
    private javax.swing.JPanel tableFrame;
    // End of variables declaration//GEN-END:variables

    private void cancel() {
        int resp = JOptionPane.showConfirmDialog(this, "Tem certeza que deseja cancelar?", "Sair", JOptionPane.YES_NO_OPTION);

        if (resp == JOptionPane.YES_OPTION) {
            this.dispose();
            
        }
    }
}
